package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartApiService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Administrator
 * @create 2020-05-25 19:36
 */
@RestController
@RequestMapping("/api/cart")
public class CartApiController {
    @Autowired
    private CartApiService cartApiService;


    //添加购物车
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public CartInfo addToCart(@PathVariable("skuId") Long skuId,
                              @PathVariable("skuNum") Integer skuNum,
                              HttpServletRequest request){
        //用户ID
        String userId = AuthContextHolder.getUserId(request);
        System.out.println("当前用户id是："+userId);
        //判断真实用户ID为NULL 使用临时用户的Id
        if(StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
            System.out.println("当前临时用户的ID："+userId);
        }
        return cartApiService.addToCart(skuId,skuNum,userId);
    }
    //去购物车结算
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request){
        //当前登录人的购物车集合
        //真实用户ID
        String userId = AuthContextHolder.getUserId(request);
        //临时用户ID
        String userTempId = AuthContextHolder.getUserTempId(request);
        System.out.println("去往购物车是的用户id:"+userId+" ==>临时："+userTempId);
        //购物车集合
        List<CartInfo> cartInfoList =  cartApiService.cartList(userId,userTempId);
        return Result.ok(cartInfoList);
    }
    //购物车之选中或取消
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable("skuId") Long skuId
            ,@PathVariable("isChecked") Integer isChecked,HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        System.out.println("=============="+userId);
        if(StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartApiService.checkCart(skuId,isChecked,userId);
        return Result.ok();
    }

    //3.根据用户id去查询被选中购物项数据
    @GetMapping("/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable("userId") Long userId){
        return cartApiService.getCartCheckedList(userId);

    }

  /*  //4.加减号
    @PostMapping("/addToCart/{skuId}/{skuNum}")
    public Result updateToCart(@PathVariable("skuId") Long skuId,
                               @PathVariable("skuNum") Integer skuNum,
                               HttpServletRequest request){
        //获得当前用户的id
        String userId = AuthContextHolder.getUserId(request);
        cartApiService.updateToCart(userId,skuId,skuNum);
        return Result.ok();
    }*/

    //5.删除'/deleteCart/' + skuId,

}
